﻿using Sunny.UI;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ScadaDAL
{
    /// <summary>
    /// 文本文档记录访问类
    /// </summary>
    public class IniCofigHelper
    {   
        /// <summary>
        /// 读取INI文件中的所有Value的值
        /// </summary>
        /// <param name="IniName"></param>
        /// <param name="SecetionName"></param>
        /// <returns>返回list集合</returns>
        public static List<string> ReadIniConfig(string Directory,string IniName,string SecetionName)
        {
            List<string> result = new List<string>();
            List<string> SectionName = new List<string>();
            string iniFilePath = "";
            //string directory = System.AppDomain.CurrentDomain.BaseDirectory;
            iniFilePath = Path.Combine(Directory, IniName);
            try
            {
                result = IniFileHelper.GetKeys(SecetionName, Path.Combine(Directory, IniName));
                for (int i = 0; i < result.Count; i++)
                {
                    SectionName.Add(IniFileHelper.IniReadValue(SecetionName, result[i], Encoding.UTF8, iniFilePath).Replace("\0", ""));
                }
            }
            catch (Exception)
            {

            }
            return SectionName;
        }
        /// <summary>
        /// 写入INI文件指定Key的数据
        /// </summary>
        /// <param name="IniName"></param>
        /// <param name="SectionName"></param>
        /// <param name="KeyName"></param>
        /// <param name="Value"></param>
        /// <returns></returns>
        public static bool WitrteIniData(string IniName, string SectionName,string KeyName,string Value)
        {
            string directory = System.AppDomain.CurrentDomain.BaseDirectory;
            string iniFilePath = Path.Combine(directory, IniName);
            try
            {
                IniFileHelper.WriteIniUF8(SectionName, KeyName, Value, iniFilePath);
                return true;
            }
            catch (Exception)
            {

                return false;
            }
            
        }
        /// <summary>
        /// 写入INI文件List集合
        /// </summary>
        /// <param name="IniName">文件名</param>
        /// <param name="SectionName">参数名</param>
        /// <param name="WirteData">数据集合</param>
        /// <returns></returns>
        public static List<string> WitrteIniData(string IniName,string SectionName, List<string>WirteData)
        {            
            string directory = System.AppDomain.CurrentDomain.BaseDirectory;
            string iniFilePath = Path.Combine(directory, IniName);
            List<string> KeyList = new List<string>();
            List<string> WirteDataResult= WirteData;
            KeyList = IniFileHelper.GetKeys(SectionName, Path.Combine(directory, IniName));
            foreach (var Keyitem in KeyList)
            {
                foreach (string WirteDataitem in WirteData)
                {
                    IniFileHelper.WriteIniUF8(SectionName, Keyitem, WirteDataitem, iniFilePath);
                }
            }
            return WirteDataResult;
        }
        /// <summary>
        /// 读取Text全部文本的方法
        /// </summary>
        /// <param name="FolderPath">读取文件夹的路径</param>
        /// <param name="LogName">读取记录日志的类型</param>
        /// <returns></returns>
        public static string ReadAllText(string FolderPath,string LogName)
        {
            DateTime dateTime = DateTime.Now;
            string directory = System.AppDomain.CurrentDomain.BaseDirectory;
            string IniName = LogName + dateTime.ToString("D") + ".ini";
            string LogIniPath = FolderPath + IniName;
            string result= File.ReadAllText(LogIniPath);
            return result;
        }
        /// <summary>
        /// 写入文本文档内容
        /// </summary>
        /// <param name="FolderPath">文件夹名称</param>
        /// <param name="LogName">日志名称</param>
        /// <param name="Value">写入的值</param>
        /// <returns></returns>
        public static bool WiriteAllText(string FolderPath, string LogName,string Value)
        {            
            DateTime dateTime = DateTime.Now;
            string directory = System.AppDomain.CurrentDomain.BaseDirectory;
            string LogIniPath = FolderPath + LogName + ".txt";
            try
            {
                File.WriteAllText(LogIniPath, Value);
                return true;
            }
            catch (Exception)
            {
                return false;
            }
            
        }
        /// <summary>
        /// 读取文本文档内容返回字符串数组
        /// </summary>
        /// <param name="FolderPath">文件夹路径</param>
        /// <param name="LogName">文本文档的名称</param>
        /// <returns></returns>
        public static string[] ReadStringText(string FolderPath, string LogName)
        {
            DateTime dateTime = DateTime.Now;
            string directory = System.AppDomain.CurrentDomain.BaseDirectory;
            string LogIniPath = FolderPath + (LogName + dateTime.ToString("D") + ".txt");
            string[] result = File.ReadAllLines(LogIniPath);
            return result;
        }
        /// <summary>
        /// 日志记录的方法（TEXT)，根据日期自动创建文本文件
        /// </summary>
        /// <param name="FolderPath">文件夹路径</param>
        /// <param name="LogText">日志内容</param>
        /// <param name="LogName">日志名称</param>
        /// <returns></returns>
        public static bool WirteApeedLog(string FolderPath, string LogText,string LogName)
        {
            bool Result = false;
            try
            {
                DateTime dateTime = DateTime.Now;
                string directory = System.AppDomain.CurrentDomain.BaseDirectory;
                string LogIniPath = FolderPath + (LogName + dateTime.ToString("D") + ".txt");               
                FileInfo fi = new FileInfo(LogIniPath);
                if (fi.Directory.Exists)
                {
                    //插入Text新的数据
                    File.AppendAllText(LogIniPath, "记录时间：" + DateTime.Now + "  记录内容： " + LogText+"\r\n");
                }
                else
                {

                    fi.Directory.Create();      //不存在目录，则创建
                    FileStream fs = new FileStream(LogIniPath, FileMode.Create, FileAccess.Write);                    
                    StreamWriter sw = new StreamWriter(fs, Encoding.UTF8);
                    sw.WriteLine(LogText, Environment.NewLine);
                    sw.Close();
                    fs.Close();
                } 
                Result=true;
            }
            catch (Exception)
            {
                Result = false;
            }
            return Result;
        }
        /// <summary>
        /// 根据键值查询Value
        /// </summary>
        /// <param name="SectionNamePartmeter">键值名称</param>
        /// <param name="KeyValue">要查询的Value名称</param>
        /// <param name="IniName">配置文件的名称</param>
        /// <returns></returns>
        public static string ReadIniValue(string SectionNamePartmeter, string KeyValue, string IniName)
        {
            string Result = "";
            string directory = System.AppDomain.CurrentDomain.BaseDirectory;
            string iniFilePath = Path.Combine(directory, IniName);
            List<string> KeyValues = new List<string>();
            KeyValues = IniFileHelper.GetKeys(SectionNamePartmeter, Path.Combine(directory, IniName));
            for (int j = 0; j < KeyValues.Count; j++)
            {
                if (KeyValues[j] == KeyValue)
                {
                    Result = IniFileHelper.GetValue(SectionNamePartmeter, KeyValues[j], iniFilePath);
                }
            }
            return Result;
        }
        /// <summary>
        /// 获取集合下的所有Key
        /// </summary>
        /// <param name="IniName"></param>
        /// <param name="KeyName"></param>
        /// <returns></returns>
        public static List<string>GetKeyName(string IniName, string KeyName)
        {
            string directory = System.AppDomain.CurrentDomain.BaseDirectory;
            string iniFilePath = Path.Combine(directory, IniName);
           return IniFileHelper.GetKeys(KeyName, iniFilePath);
        }
    }
}
